package com.video.user.controller;


import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.video.common.enumeration.AppHttpCodeEnum;
import com.video.common.result.PageResult;
import com.video.common.result.ResponseResult;
import com.video.common.utils.UserContext;
import com.video.user.domain.dto.LoginDto;
import com.video.user.domain.dto.QueryPageDto;
import com.video.user.domain.entity.AdminUser;
import com.video.user.domain.entity.AppUser;
import com.video.user.service.IAdminUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 管理用户表 前端控制器
 * </p>
 *
 * @author la
 * @since 2024-11-04
 */
@RestController
@RequestMapping("/admin-user")
@Api(tags = "管理系统用户")
@RequiredArgsConstructor
public class AdminUserController {

    private final IAdminUserService adminUserService;

    /*
     * 登录接口
     * */
    @ApiOperation("管理登录接口")
    @PostMapping("/login")
    public ResponseResult login(@RequestBody LoginDto loginDto) {
        ResponseResult result = adminUserService.login(loginDto);
        return result;
    }

    /*
     * 获取用户信息
     * */
    @PostMapping("/info")
    @ApiOperation("获取用户信息接口")
    public ResponseResult userInfo() {
        // 根据网关信息和拦截器信息中获取token,解析出id
        Long id = UserContext.getUser();
        AdminUser user = adminUserService.lambdaQuery()
                .eq(id != null, AdminUser::getId, id)
                .one();
        return ResponseResult.okResult(user);
    }

    // 获取用户列表
    @PostMapping("/list")
    public ResponseResult list(@RequestBody QueryPageDto dto) {
        PageResult res = adminUserService.getAppUserList(dto);
        return ResponseResult.okResult(res);
    }

    /*
     * 新增或编辑用户列表
     * */
    @PostMapping("/updateAppUser")
    public ResponseResult updateAppUser(@RequestBody AppUser user) {
        adminUserService.updateUser(user);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /*
     * 删除用户
     * */
    @DeleteMapping("/{id}")
    public ResponseResult delAppUser(@PathVariable Long id) {
        // 参数校验
        if (id == null) return ResponseResult.errorResult(AppHttpCodeEnum.ARGUMENT_ERROR);
        // 执行数据库操作
        Db.removeById(id, AppUser.class);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 获取管理员列表
     */
    @PostMapping("/adminUserList")
    public ResponseResult adminList(@RequestBody QueryPageDto dto) {
        PageResult res = adminUserService.getAdminUserList(dto);
        return ResponseResult.okResult(res);
    }

    /**
     * 新增或编辑管理员
     */
    @PostMapping("/updateAdminUser")
    public ResponseResult updateAppUser(@RequestBody AdminUser user) {
        adminUserService.updateAdminUser(user);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }



    /**
     * 删除管理员
     * @param id
     * @return
     */
    @DeleteMapping("/admin/{id}")
    public ResponseResult delAdminUser(@PathVariable Long id) {
        // 参数校验
        if (id == null) return ResponseResult.errorResult(AppHttpCodeEnum.ARGUMENT_ERROR);
        // 执行数据库操作
        Db.removeById(id, AdminUser.class);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
